package com.seanliao.nav.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seanliao.nav.common.domain.vo.RoleVO;
import com.seanliao.nav.common.domain.vo.UserVO;
import com.seanliao.nav.entity.SysRole;
import com.seanliao.nav.entity.SysUser;
import com.seanliao.nav.entity.SysUserRole;
import com.seanliao.nav.mapper.SysUserMapper;
import com.seanliao.nav.service.ISysRoleService;
import com.seanliao.nav.service.ISysUserRoleService;
import com.seanliao.nav.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表  服务实现类
 * </p>
 *
 * @author Sean_Liao
 * @since 2024-01-23
 */
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    private final ISysUserRoleService userRoleService;
    private final ISysRoleService roleService;

    @Override
    public Page<UserVO> pageUser(Page<UserVO> pp, SysUser user) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<SysUser>()
                .like(StrUtil.isNotBlank(user.getUsername()), SysUser::getUsername, user.getUsername())
                .like(StrUtil.isNotBlank(user.getNickname()), SysUser::getNickname, user.getNickname());
        this.getBaseMapper().pageUser(pp, wrapper);
        this.handleRole(pp.getRecords());
        return pp;
    }

    @Override
    public Page<UserVO> pageUserByRole(Page<UserVO> pp, SysUser user, SysRole role) {
        QueryWrapper<UserVO> wrapper = new QueryWrapper<UserVO>()
                .like("sr.name", role.getName())
                .like(StrUtil.isNotBlank(user.getUsername()), "su.username", user.getUsername())
                .like(StrUtil.isNotBlank(user.getNickname()), "su.nickname", user.getNickname());
        this.getBaseMapper().pageUserByRole(pp, wrapper);
        this.handleRole(pp.getRecords());
        return pp;
    }


    /**
     * 处理角色
     */
    private void handleRole(List<UserVO> userList) {
        if (userList.isEmpty()) {
            return;
        }
        List<SysRole> roles = roleService.list();
        Map<Long, SysRole> roleMap = roles.stream().collect(Collectors.toMap(SysRole::getId, v -> v));
        // 遍历用户
        userList.forEach(userVO -> {
            List<SysUserRole> userRoles = userRoleService.list(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getUserId, userVO.getId()));
            List<RoleVO> roleVOs = new ArrayList<>(userRoles.size());
            userRoles.forEach(
                    userRole -> {
                        SysRole role = roleMap.get(userRole.getRoleId());
                        if (role != null) {
                            RoleVO roleVO = new RoleVO();
                            roleVO.setId(role.getId());
                            roleVO.setCode(role.getCode());
                            roleVO.setName(role.getName());
                            roleVOs.add(roleVO);
                        }
                    }
            );
            userVO.setRoles(roleVOs);
        });

    }

}
